#include "AppMenuCategoryPanel.h"

#include <QBoxLayout>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QLabel>
#include <QPushButton>
#include <QStylePainter>

#include "../CMD/CmdMgr.h"
#include "AppMenuItem.h"
#include "AppMenuPanel.h"
#include "core/StyleMgr.h"

AppMenuCategoryPanel::AppMenuCategoryPanel(QJsonObject* data, QWidget* parent)
    : QWidget(parent) {
  construct(data);
  setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
  setFixedWidth(170);
}

void AppMenuCategoryPanel::addCmd(int id) {
  if (m_category.id != 11007) return;

  Command* cmd = CmdMgr::instance().command(id);
  if (!cmd) return;
  AppMenuItem* item = new AppMenuItem(id, this);
  item->setInFav(true);

  QVBoxLayout* l = dynamic_cast<QVBoxLayout*>(layout());
  if (m_category.items.isEmpty()) m_commonWidget->setVisible(false);

  l->insertWidget(l->count() - 2, item);
  m_category.items.push_back(item);
}

void AppMenuCategoryPanel::removeCmd(int id) {
  if (m_category.id != 11007) return;

  for (auto iter = m_category.items.begin(); iter != m_category.items.end();
       ++iter) {
    if ((*iter)->cmdId() == id) {
      AppMenuItem* item = *iter;
      m_category.items.erase(iter);
      QVBoxLayout* l = dynamic_cast<QVBoxLayout*>(layout());
      l->removeWidget(item);
      item->setParent(nullptr);
      item->deleteLater();
      break;
    }
  }

  if (m_category.items.isEmpty()) m_commonWidget->setVisible(true);
}

bool AppMenuCategoryPanel::hasCmd(int id) {
  for (auto cmd : m_category.items) {
    if (cmd->cmdId() == id) return true;
  }
  return false;
}

bool AppMenuCategoryPanel::isFavePanel() { return m_category.id == 11007; }

bool AppMenuCategoryPanel::isIasPanel() { return m_category.id == 11009; }

bool AppMenuCategoryPanel::isRedPoint() {
  for (auto cmd : m_category.items) {
    if (cmd && cmd->isRedPoint()) {
      return true;
    }
  }
  return false;
}

void AppMenuCategoryPanel::removeRedPoint() {
  for (auto cmd : m_category.items) {
    for (auto cmd : m_category.items) {
      if (cmd && cmd->isRedPoint()) {
        CmdMgr::instance().removePageRedPoint(cmd->cmdId());
      }
    }
  }
}

void AppMenuCategoryPanel::construct(QJsonObject* data) {
  if (data == nullptr) return;

  m_category.id = (*data)["id"].toInt();
  m_category.title = (*data)["title"].toString();

  QVBoxLayout* mainLayout = new QVBoxLayout();
  mainLayout->setContentsMargins(10, 10, 10, 0);
  mainLayout->setSpacing(2);

  QString title =
      "<p style=\"font-size:15px;font-weight:bold;color:#F9C152\">%1</p>";
  title = title.arg(m_category.title);

  QLabel* titleLabel = new QLabel(title);
  // To make the title align with the item
  QHBoxLayout* titleLayout = new QHBoxLayout();
  titleLayout->setContentsMargins(0, 0, 0, 0);
  titleLayout->addSpacing(5);
  titleLayout->addWidget(titleLabel);
  mainLayout->addLayout(titleLayout);

  if (isFavePanel())  // 常用功能
  {
    m_commonWidget = new QWidget(this);
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setContentsMargins(5, 10, 0, 0);
    QString tip =
        "<p style=\"font-size:12px; font-weight:bold; color:#FFEBC8\">%1</p>";
    tip = tip.arg(tr("点击功能右侧加号添加"));
    QLabel* label = new QLabel(tip);
    layout->addWidget(label, 0, Qt::AlignLeft);

    layout->addSpacing(30);
    QPushButton* btn = new QPushButton();
    btn->setStyleSheet(
        QString(
            ":hover{\nbackground-color:#308376;\n}\nQPushButton{\nbackground-"
            "color:#%1;\nborder-radius:3px;\ncolor:#FFEBC8;\nfont-size:12px\n}")
            .arg(NAME_COLOR("MenuTitleHover").rgb(), 0, 16));
    btn->setText(tr("前往首页"));
    btn->setFixedSize(92, 30);
    layout->addWidget(btn, 0, Qt::AlignHCenter);

    m_commonWidget->setLayout(layout);
    mainLayout->addWidget(m_commonWidget);
  } else if (isIasPanel()) {
    QJsonDocument doc =
        QJsonDocument::fromJson(CmdMgr::instance().getIasMenu().toUtf8());
    if (!doc.isNull()) {
      QJsonArray subItems = doc.array();
      for (QJsonValue value : subItems) {
        QJsonObject obj = value.toObject();
        Command* cmd = CmdMgr::instance().command(obj["id"].toInt());
        if (!cmd || !cmd->isAuthed()) continue;

        AppMenuItem* item = new AppMenuItem(&obj, this);
        mainLayout->addWidget(item);
        m_category.items.push_back(item);
      }
    }
  } else {
    QJsonArray subItems = (*data)["submenu"].toArray();
    for (QJsonValue value : subItems) {
      QJsonObject obj = value.toObject();
      Command* cmd = CmdMgr::instance().command(obj["id"].toInt());
      if (!cmd || !cmd->isAuthed() || cmd->shouldHide()) continue;

      AppMenuItem* item = new AppMenuItem(&obj, nullptr);
      if (!item->isEnabled()) {
        item->deleteLater();
        continue;
      }

      mainLayout->addWidget(item);
      m_category.items.push_back(item);
    }
  }

  mainLayout->addStretch();
  setLayout(mainLayout);
  if (isFavePanel()) {
    AppMenuPanel::g_menuPanel()->setFavPanel(this);
  }
}

void AppMenuCategoryPanel::paintEvent(QPaintEvent* event) {
  QStylePainter painter(this);

  QVBoxLayout* mainLayout = (QVBoxLayout*)layout();
  if (mainLayout) {
    QHBoxLayout* item = (QHBoxLayout*)mainLayout->itemAt(0);
    if (!item) return;
    QSize size = item->sizeHint();
    QPen pen(NAME_COLOR("BasicBtn_Filter_Tittle"), 1);
    pen.setWidth(1);
    painter.setPen(pen);
    painter.drawLine(QPoint(rect().left() + 1, rect().top() + size.height()),
                     QPoint(rect().left() + 1, rect().bottom() - 27));
  }
}
